package com.luoqiuy.poet.service.redisService.impl;

import com.luoqiuy.poet.entity.*;
import com.luoqiuy.poet.util.RedisUtil;
import com.luoqiuy.poet.service.redisService.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<Poet> loadPoetsByAuthorId(int id) {
        Object obj = redisUtil.get(String.valueOf(id));

        return ( List<Poet>)obj;
    }

    @Override
    public boolean writePoetCacheByAuthorId(int id, List<Poet> books) {
        return redisUtil.set(String.valueOf(id), books);
    }

    @Override
    public Poet loadCachePoetByPoetId(int id) {

        return (Poet)redisUtil.get(String.valueOf(id) + ":poet");
    }

    @Override
    public boolean writePoetCacheByPoetId(int id, Poet poet) {

        return redisUtil.set(String.valueOf(id) + ":poet", poet);
    }

    @Override
    public List<Content> loadCacheContentByPoetId(int poetId) {
        Object obj = redisUtil.get(String.valueOf(poetId) + ":content");
        return ( List<Content>)obj;
    }

    @Override
    public boolean writeContentCacheByPoetId(int poetId, List<Content> contents) {
        return redisUtil.set(String.valueOf(poetId) + ":content", contents);
    }

    @Override
    public boolean writeCompleteCacheContents(int poetId, List<Content> contents) {
        return redisUtil.set(String.valueOf(poetId) + ":completeContent", contents);
    }

    @Override
    public List<Content> loadCompleteCacheContentByPoetId(int poetId) {
        Object obj = redisUtil.get(String.valueOf(poetId) + ":completeContent");
        return ( List<Content>)obj;
    }

    @Override
    public Integer loadCacheVisitCount(int goalId) {
        Object obj = redisUtil.get(String.valueOf(goalId) + ":visitCount");
        return (Integer)obj;
    }

    @Override
    public boolean writeVisitCountByUserId(int goalId) {
        boolean flag = redisUtil.set(String.valueOf(goalId) + ":visitCount", 0);
        redisUtil.incr(String.valueOf(goalId) + ":visitCount",1);
        return flag;
    }

    @Override
    public User loadCacheUserByUserId(int goalId) {

        return (User) redisUtil.get(String.valueOf(goalId) + ":userAllInfo");
    }

    @Override
    public boolean writeUserCacheByUserId(int goalId, User goalUser) {
        return redisUtil.set(String.valueOf(goalId) + ":userAllInfo", goalUser);
    }

    @Override
    public void writeFollowCacheByUserId(int goalId, User info) {
        redisUtil.sSet(String.valueOf(goalId) + ":follow", info);
    }

    @Override
    public List<User> loadCacheFollowByUserId(int curUserId) {
        Set<Object> res =  redisUtil.sGet(String.valueOf(curUserId) + ":follow");
        List<User> list = new ArrayList<>();
        res.forEach(re  ->
            list.add((User)re)
        );
        return list;
    }

    @Override
    public List<User> loadCacheComonFollowByAllId(int goalId, int curUserId) {
        Set<Object> res =  redisUtil.sGet(String.valueOf(goalId) + "And" + String.valueOf(curUserId) + "commonFollow" );
        if (null == res)
            return null;
        List<User> list = new ArrayList<>();
        res.forEach(re  ->
                list.add((User)re)
        );
        return list;
    }

    @Override
    public List<User> getCacheComonFollowByAllId(int goalId, int curUserId) {
        Set<Object> res =  redisUtil.intersect(String.valueOf(goalId), String.valueOf(curUserId));
        List<User> list = new ArrayList<>();
        res.forEach(re  ->
                list.add((User)re)
        );
        return list;
    }

    @Override
    public Introduction loadCacheIntroByUserId(int goalId) {
        Object obj = redisUtil.get(String.valueOf(goalId) + ":intro");
        return (Introduction)obj;
    }

    @Override
    public boolean writeCacheIntroByUserId(int goalId, Introduction introduction) {

        return redisUtil.set(String.valueOf(goalId)+ ":intro", introduction);
    }

    @Override
    public List<VisitMessage> loadCacheVisitMessByUserId(int goalId) {
        Object obj = redisUtil.get(String.valueOf(goalId) + ":visitMess");
        return (List<VisitMessage>)obj;
    }

    @Override
    public boolean writeCacheVisitMessByOwnerId(int goalId, List<VisitMessage> messages) {
        return redisUtil.set(String.valueOf(goalId) + ":visitMess", messages);
    }
}
